package com.pn.taikao.service.impl;

import com.pn.taikao.entity.PaperStudent;
import com.pn.taikao.mapper.PaperStudentMapper;
import com.pn.taikao.service.PaperStudentService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PaperStudentServiceImpl implements PaperStudentService {

    private final PaperStudentMapper paperStudentMapper;

    public PaperStudentServiceImpl(PaperStudentMapper paperStudentMapper) {
        this.paperStudentMapper = paperStudentMapper;
    }

    @Override
    public int addPaperStudent(PaperStudent paperStudent) {
        paperStudent.setCreateTime(LocalDateTime.now());
        paperStudent.setUpdateTime(LocalDateTime.now());
        return paperStudentMapper.insert(paperStudent);
    }

    @Override
    public int addPaperStudents(List<PaperStudent> paperStudents) {
        if (paperStudents == null || paperStudents.isEmpty()) {
            return 0;
        }
        LocalDateTime now = LocalDateTime.now();
        for (PaperStudent ps : paperStudents) {
            ps.setCreateTime(now);
            ps.setUpdateTime(now);
        }
        return paperStudentMapper.insertBatch(paperStudents);
    }

    @Override
    public int deleteByPaperId(Long paperId) {
        return paperStudentMapper.deleteByPaperId(paperId);
    }

    @Override
    public int deleteByPaperIdAndStudentId(Long paperId, Long studentId) {
        return paperStudentMapper.deleteByPaperIdAndStudentId(paperId, studentId);
    }

    @Override
    public List<PaperStudent> getByPaperId(Long paperId) {
        return paperStudentMapper.selectByPaperId(paperId);
    }

    @Override
    public List<PaperStudent> getByStudentId(Long studentId) {
        return paperStudentMapper.selectByStudentId(studentId);
    }

    @Override
    public boolean isPublishedToStudent(Long paperId, Long studentId) {
        return paperStudentMapper.existsByPaperIdAndStudentId(paperId, studentId);
    }
}


